<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/jsp/comm_css_include.jsp" %>
<!-- jqgrid-->
<link href="${basePath}/css/plugins/jqgriddemo/css/ui.jqgrid-bootstrap.min.css" rel="stylesheet" />
<link href="${basePath}/js/plugins/chosen/chosen.css" rel="stylesheet" type="text/css"/>
<!doctype html>
<html>
<head>
</head>
<body class="gray-bg">
	<div class="creditbody">
	    <div class="iboxtitle">
	        <span><i class="glyphicon glyphicon-map-marker"></i>&nbsp;业务受理 >业务申请 >查征信派单</span>
	        <a href="javascript:void(0)"></a>
	    </div>
	    <div class="ibox-content ibox-con">
	    	<div class="buttonfixed">
	    		<button type="button" class="btn dpbtn_blue btnaplly" onclick="allocation();">确定派单</button>
	    	</div>
            <table cellpadding="0" cellspacing="0" border="0" class="buyinfor-table zx-addtable">
            	<tr>
            		<td align="right">
						<label class="control-label np">当前部门：</label>
					</td>
					<td>
						<input id="" name=""  type="text" class="form-control" readonly="readonly" value="${current_user_key.orgName}">
					</td>
					<td align="right">
						<label class="control-label np">部门主管：</label>
					</td>
					<td>
                       <input id="" name=""  type="text" class="form-control" readonly="readonly" value="${current_user_key.uname}">
					</td>
				</tr>
				<tr>	
					<td align="right">
						<label class="control-label np">当前未分配客户：</label>
					</td>
					<td>
                        <input class="form-control" readonly="readonly"  id="userCount" name="userCount"/>
					</td>
					<td align="right">
						<label class="control-label np">当前所处银行：</label>
					</td>
					<td class="td-width">
                        <select class="form-control" id="loanBank" name="loanBank" >
                        	<option></option>
                        </select>
					</td>
				</tr>
            </table>
            <div class="average-man">平均每个业务员可领到<span id="avg" name="avg"></span>单</div>
			<div class="send-tab" id="send-tab">
	           	<ul class="send-orderul">
	           		<li class="send-active" id="randomAllocation"><i class="fa fa-dot-circle-o"></i>随机派单</li>
	           		<li id="randomForce"><i class="fa fa-circle-o"></i>强制指定派单</li>
	           	</ul>
           		<div id="send-content">
	           		<div class="send-div" style="display: block;">
	           			<ul class="send-numa">
	           				<li>单次分派客户数：</li>
	           				<li><input type="number" min="1"  max="20" value="0" id="allocationNum" name="allocationNum" class="form-control" required="" onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');this.value=this.value.replace('.','');"/></li>
	           				<li>(1-100的整数)</li>
	           			</ul>	
	           		</div>
	           		<div class="send-div"> 
	           			<ul class="send-numa">
	           				<li>设定操作员：</li>
	           				<li>
	           					<select id="uname" name="uname" class="select-control" style="width: 180px">
	           						&nbsp;
	           					</select>
	           				</li>
	           			</ul>
				     	<div class="jqGrid_wrapper">
				         	<table id="table_list_1"></table>
				         	<div id="pager_list_1"></div>
				    	</div>
	           		</div>
	           	</div>	
           	</div>
	     </div>     
	</div>

	<!-- public quote -->
	<%@include file="/jsp/comm_js_include.jsp" %>
	<!-- jqGrid -->
    <script type="text/javascript" charset="utf-8" src="${basePath}/js/plugins/jqgriddemo/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" charset="utf-8" src="${basePath}/js/plugins/jqgriddemo/js/grid.locale-cn.min.js"></script>
	
	<script type="text/javascript" charset="utf-8" src="${basePath}/js/plugins/chosen/chosen.jquery.js"></script>

	<script type="text/javascript">
	
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	
	var $thisIndex = 0;

	jQuery(function(){
        initData();
	})
	
	var initData = function()
	{
	   winWidth = $(window).width();
   	   winHeight = $(window).height() / 2.3;
	   var win = winWidth / 8 - 30;

 	   $.jgrid.defaults.styleUI = 'Bootstrap';

	  jQuery("#table_list_1").jqGrid
	  ({
		  show:false,
		  url:'${basePath}/YwShenQingZxController/getCreditQueryList',
	      datatype : "json",
	      colNames : [ 'replaceId','经销商','部门', '业务员','客户名','身份证','银行', '经办人'],
	      colModel : [
	                   {name: 'replaceId', index: '', hidden:true,sortable: false},
	                   {name: 'dealerName', index: '', hidden:false,sortable: false},
	                   {name: 'orgName', index: '', hidden:false},
	                   {name: 'customerName',index: '',width: win,align: "center",sortable: false},
	                   {name: 'cusName',index: '',width: win,align: "center",sortable: false},
	                   {name: 'cusNo',index: '',width: win,align: "center",sortable: false},
	                   {name: 'bankCode',index: '',width: win,align: "center",sortable: false},
	                   {name: 'creatorName',index: '',width: win,align: "center",sortable: false}
	                 ],
	        mtype : "post",
	        height: winHeight,
	        autowidth: false,
	        shrinkToFit: false,
	        rowNum: 15,
	        rowList: [15, 30, 45],
	        viewrecords: true,
	        pager: "#pager_list_1",
	        hidegrid: false,
	        multiselect : true,
	        altRows:true,
            altclass:'someClass',
	        jsonReader : {
	      	  root:"result.rows",
	      	  page: "result.page",
	      	  total: "result.total",
	      	  records: "result.records",
	      	  repeatitems: false,
	      	  id: "replaceId"
	        },
	        loadComplete:function(datas){
	      	    if (datas.flag == "100100") {
	                layer.msg("查询成功!", {icon: 6, time: 1000});
	                var uname = $('#uname').val();
	                getOpList();
	                return;
	            }
		      	  if(datas.flag == '100802') {
	 				  top.location.href = thisLoginUrl;
	 				  return;
	 			  }
	            layer.msg(datas.message, {icon: 2});
	        }
	    });
	  
	  $.dpAjax({
		   show:false,
	   		url:'${basePath}/YwShenQingZxController/getLoanBank',
	   		data:{},
			success: function(datas){
		 	  if(datas.flag == "100100"){
		 		
		 		  var bankMaps = datas.bankMaps;
		 		  if(bankMaps)
		 		  {
		 			  var objs = '';
		 			  for(var key in bankMaps)
		 			  {
		 				 objs += '<option value="'+key+'">'+bankMaps[key]+'</option>';
		 			  }
		 			  $('#loanBank').html(objs);
		 			  return;
		 		  }
		 		  var objs = '<option value="无分配银行">无分配银行</option>';
		 		  $('#loanBank').html(objs);
				  return;
			  }
			  if(datas.flag == "100200"){
				  alert(JSON.stringify(datas));
	              return;
	          }
			  layer.msg(datas.message, {icon: 2});
			}
	   });
	}
	
	var getOpList = function()
	{
		//设定操作员查询
	    $.dpAjax({
	   		url:'${basePath}/YwShenQingZxController/getOpList',
	   		data:{},
			success: function(datas){
		 	  if(datas.flag == "100100"){
		 		
		 		  var d = datas.result;
		 		  
		 		  var objs = '';
		 		  
		 		  for(var i=0;i<d.length;i++)
		 		  {
		 			  objs += '<option value="'+d[i].replaceId+'">'+d[i].uname+'</option>';
		 		  }
		 		  
		 		  $('#uname').append(objs);
		 		  $("#uname").chosen();
		 		  var rowNum = $("#table_list_1").jqGrid('getGridParam','records');
		 		  
		 		  var avg = rowNum / d.length;
		 		  
		 		  //客户数赋值
		 		  $('#userCount').val(rowNum);
		 		  
		 		  if(avg > 0 && avg < 1)
		 		  {
		 			 avg = 1;
		 		  }
		 		  else
		 		  {
		 			 avg = parseInt(avg);
		 		  }
		 		  $('#avg').html(avg);
		 		  
				  return;
			  }
			  if(datas.flag == "100200"){
				  alert(JSON.stringify(datas));
	              return;
	          }
			  layer.msg(datas.message, {icon: 2});
			}
	   });
	}
	
	//确定分单
	var allocation = function()
	{
		//询问框
		layer.confirm('派单已就绪，是否确认执行？操作人：<span style="color:#BD1723;">${current_user_key.uname}</span>', {
		  btn: ['确认','取消'] //按钮
		}, function(){
			layer.closeAll();
			//随机派单
			if($thisIndex == 0)
			{
				 var allocationNum = $('#allocationNum').val();
				 if(allocationNum == "" || allocationNum == 0)
				 {
					 layer.msg("请输入单次分配客户数！", {icon: 5});
					 return;
				 }
				 var rowNum = $("#table_list_1").jqGrid('getGridParam','records');
				 
				 if(allocationNum > rowNum)
				 {
					 layer.msg("目前只有"+rowNum+"单可分！", {icon: 5});
					 $('#allocationNum').val(rowNum);
					 return;
				 }
				 
				 var uname = $("#uname").find("option:selected").text();
				 var uid = $('#uname').val();
					
				 $.dpAjax({
				   		url:'${basePath}/YwShenQingZxController/saveZhengXinData',
				   		data:{
				   			"zxFenPeiCode":"0",
				   			"allocationNum":allocationNum
				   		},
						success: function(datas){
					 	  if(datas.flag == "100100"){
					 		  layer.msg("随机派单成功！", {icon: 6});
					 		  refresh();
							  return;
						  }
						  if(datas.flag == "100200"){
							  alert(JSON.stringify(datas));
				              return;
				          }
						  layer.msg(datas.message, {icon: 2});
						}
				 });
				return;
			}
			
			//强制派单
			var rowData = jQuery('#table_list_1').jqGrid('getGridParam','selarrrow');  
			if(rowData.length < 1)
	    	{  
	    		layer.msg("请选择要分配的信息！", {icon: 5});
				return;
	    	}
			
			var replaceId = "";
			
			for(var i=0;i<rowData.length;i++)
	    	{
	    		var mainId = jQuery('#table_list_1').jqGrid('getCell',rowData[i],'replaceId'); 
	    		
	    		replaceId += mainId+",";
	    	}
			
			if(replaceId != "")
			{
				replaceId = replaceId.substring(0,replaceId.length-1);
			}
			
			var uname = $("#uname").find("option:selected").text();
			var uid = $('#uname').val();
			
			
			$.dpAjax({
		   		url:'${basePath}/YwShenQingZxController/saveZhengXinData',
		   		data:{
		   			"zxFenPeiCode":"1",
		   			"uname":uname,
		   			"uid":uid,
		   			"replaceId":replaceId
		   		},
				success: function(datas){
			 	  if(datas.flag == "100100"){
			 		  layer.msg("强制指定派单成功！", {icon: 6});
			 		  refresh();
					  return;
				  }
				  if(datas.flag == "100200"){
					  alert(JSON.stringify(datas));
		              return;
		          }
				  layer.msg(datas.message, {icon: 2});
				}
		   	});
			
		}, function(){
			layer.msg('已取消本次操作', {icon: 6});
		});
	}
	
	window.onresize = function()
    {
    	$("#table_list_1").setGridWidth($(window).width()-80);
    }

    var refresh = function()
    {
    	$("#table_list_1").jqGrid("setGridParam", { search: true }).trigger("reloadGrid");
    }
</script>
</body>
</html>